// pages/recharge-page/recharge-page.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyList:[],
    rechargeMoney:0,
    hadnsel:0,
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options&&options.money){
      this.setData({
        money:options.money
      })
    }
    
    this.getList()
  },
  getList() {
    let that = this;
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY21;
    let data = {
      appid: getApp().globalData.appId,
      sessionId: wx.getStorageSync('session_id')
    };
    GET(url, data, (ret) => {
      if (ret.statusCode == 200) {
        console.log(ret.data.lproduct)
        let data = ret.data.lproduct
        that.setData({
          moneyList: data,
          rechargeMoney: data[0].price,
          hadnsel: data[0].hadnsel,
          toAccount: parseInt(data[0].price) + parseInt(data[0].hadnsel)
        })
      } else {
        wx.showToast({
          title: '数据走丢啦，请稍后再试',
          icon: 'none'
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  chooseMoney(e){
    console.log(e.currentTarget.dataset.price)
    let price = e.currentTarget.dataset.price
    let hadnsel = e.currentTarget.dataset.hadnsel
    let total = parseInt(price) + parseInt(hadnsel)
    this.setData({
      rechargeMoney: e.currentTarget.dataset.price,
      hadnsel: hadnsel,
      toAccount: total,
      id: e.currentTarget.dataset.id
    })
  },
  //充值
  recharge(){
    let id =''
    this.data.id ? id = this.data.id : id =this.data.moneyList[0].id

    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY22;
    let data = {
      appid: getApp().globalData.appId,
      sessionId: wx.getStorageSync('session_id'),
      productId: id
    };
    GET(url, data, (ret) => {
      if (ret.statusCode == 200) {
        console.log(ret)
        wx.requestPayment({
          timeStamp: ret.data.pay.timeStamp,
          nonceStr: ret.data.pay.nonceStr,
          package: ret.data.pay.package,
          signType: 'MD5',
          paySign: ret.data.pay.paySign,
          success(res) { 
            console.log(res)
          },
          fail(res) {
            wx.showToast({
              title: '已取消!',
              icon:'none'
            })
           }
        })
      } else {
        wx.showToast({
          title: '数据走丢啦，请稍后再试',
          icon: 'none',
          icon:'none'
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})